package ser;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class V extends HttpServlet {
	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
//	   /img/ValidateCodeServlet
		response.setContentType("image/gif");
		OutputStream out =response.getOutputStream();
		
		BufferedImage img=getImg();
		ImageIO.write(img, "gif", out);
		
	}

	private BufferedImage getImg() {
		//做成工具类
		BufferedImage img=new BufferedImage(200,40,BufferedImage.TYPE_INT_RGB);
		String s ="ABCDEFGHIJKLMNOPQRSTUVWXYZ3456789";
		Graphics g =img.getGraphics();
		g.setColor(Color.black);
		g.fillRect(0, 0, 200, 40);
		Random r = new Random();
		for(int i =0;i<4;i++){
			char c = s.charAt(r.nextInt(s.length()));
			g.setColor(randomColor(r));
			g.setFont(new Font("Arial",Font.BOLD|Font.ITALIC,36));
			g.drawString(String.valueOf(c), 50*i+7, 38);
		}
		return img;
	}

	private Color randomColor(Random r) {
		return new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
	}
}
